/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.DelFlagEnum;
import com.ykht.proj.coupon.entity.FxCmsColumn;
import com.ykht.proj.coupon.entity.FxCmsColumnType;
import com.ykht.proj.coupon.entity.FxCmsType;
import com.ykht.proj.coupon.service.FxCmsColumnService;
import com.ykht.proj.coupon.vo.FxCmsColumnVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 文章栏目
 *
 * @author proj code generator
 * @date 2021-04-25 18:47:54
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxcmscolumn" )
@Api(value = "fxcmscolumn", tags = "文章栏目管理")
@Slf4j
public class FxCmsColumnController {

    private final  FxCmsColumnService fxCmsColumnService;

    /**
     * 文章栏目分页查询
     * @param page 分页对象
     * @param fxCmsColumnVo 文章栏目
     * @return
     */
    @ApiOperation(value = "文章栏目分页查询", notes = "文章栏目分页查询")
    @GetMapping("/page" )
    public R getFxCmsColumnPage(Page page, FxCmsColumnVo fxCmsColumnVo) {
		log.info("分页查询:{}+文章栏目:{}", page, fxCmsColumnVo);
		IPage<FxCmsColumnVo> iPage = fxCmsColumnService.getFxCmsColumnPage(page, fxCmsColumnVo);
        return R.ok(iPage);
    }


	/**
	 * 文章栏目列表上边统计
	 * @param page 分页对象
	 * @param fxCmsColumnVo 文章栏目
	 * @return
	 */
	@ApiOperation(value = "文章栏目列表上边统计", notes = "文章栏目列表上边统计")
	@GetMapping("/getFxCmsColumnCount" )
	public R getFxCmsColumnCount(FxCmsColumnVo fxCmsColumnVo) {
		log.info("文章栏目列表上边统计:{}", fxCmsColumnVo);
		List<FxCmsColumn> columnList = fxCmsColumnService.list(Wrappers.lambdaQuery(FxCmsColumn.class)
				.like(!StringUtil.isEmpty(fxCmsColumnVo.getName()), FxCmsColumn::getName, fxCmsColumnVo.getName())
				.eq(null != fxCmsColumnVo.getSort(), FxCmsColumn::getStatus, fxCmsColumnVo.getSort()));
		HashMap<String, Long> hashMap = new HashMap<>();
		if (null != columnList && columnList.size() > 0){
			hashMap.put("whole", Long.valueOf(columnList.size()));
			long upperCount = columnList.stream().filter(itme -> itme.getStatus().equals(0)).count();
			hashMap.put("upperCount", upperCount);
			hashMap.put("lowerCount", (columnList.size() - upperCount));
		}
		return R.ok(hashMap);
	}



	/**
     * 通过id查询文章栏目
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R<FxCmsColumn> getById(@PathVariable("id" ) Long id) {
		return R.ok(fxCmsColumnService.getById(id));
    }

    /**
     * 新增文章栏目
     * @param fxCmsColumn 文章栏目
     * @return R
     */
    @ApiOperation(value = "新增文章栏目", notes = "新增文章栏目")
    @SysLog("新增文章栏目" )
    @PostMapping
    public R save(@RequestBody FxCmsColumn fxCmsColumn) {
		log.info("新增文章栏目:{}", fxCmsColumn);
		FxCmsColumn sort = fxCmsColumnService.getOne(Wrappers.lambdaQuery(FxCmsColumn.class)
				.eq(FxCmsColumn::getSort, fxCmsColumn.getSort())
				.eq(FxCmsColumn::getDelFlag, DelFlagEnum.usable.getValue()));
		if (null != sort) {
			return R.failed("排序数字已存在！");
		}
		FxCmsColumn name = fxCmsColumnService.getOne(Wrappers.lambdaQuery(FxCmsColumn.class)
				.eq(FxCmsColumn::getName, fxCmsColumn.getName())
				.eq(FxCmsColumn::getDelFlag, DelFlagEnum.usable.getValue()));
		if (null != name) {
			return R.failed("栏目名称已存在！");
		}
		fxCmsColumn.setCreateTime(LocalDateTime.now());
		fxCmsColumn.setCreateUser(SecurityUtils.getUserId());
		fxCmsColumn.setUpdateUser(SecurityUtils.getUserId());
		fxCmsColumn.setUpdateTime(LocalDateTime.now());
		fxCmsColumn.setDefFlag(0);
        return R.ok(fxCmsColumnService.save(fxCmsColumn));
    }

	@ApiOperation(value = "校验 文章栏目(栏目名称, 排序数字)")
	@GetMapping("/checkFxCmsColumn")
	public R checkFxCmsColumn(FxCmsColumn fxCmsColumn) {
		log.info("->校验 文章栏目(栏目名称, 排序数字){}", fxCmsColumn);
		FxCmsColumn one = fxCmsColumnService.getOne(Wrappers.lambdaQuery(FxCmsColumn.class)
				.eq(!StringUtil.isEmpty(fxCmsColumn.getName()), FxCmsColumn::getName, fxCmsColumn.getName())
				.eq(null != fxCmsColumn.getSort(), FxCmsColumn::getSort, fxCmsColumn.getSort()));
		return R.ok(one == null ? true : one.getId().equals(fxCmsColumn.getId()) ? true : false);
	}


	/**
     * 上下架 文章栏目
     * @param fxCmsColumnVo 文章栏目
     * @return R
     */
    @ApiOperation(value = "上下架 文章栏目", notes = "上下架 文章栏目")
    @SysLog("上下架 文章栏目" )
    @PostMapping("updateStatus")
    public R updateStatus(@RequestBody FxCmsColumn fxCmsColumn) {
		fxCmsColumn.setUpdateUser(SecurityUtils.getUserId());
		fxCmsColumn.setUpdateTime(LocalDateTime.now());
        return R.ok(fxCmsColumnService.updateById(fxCmsColumn));
    }


	/**
	 * 修改文章栏目
	 * @param fxCmsColumnVo 文章栏目
	 * @return R
	 */
	@ApiOperation(value = "修改文章栏目", notes = "修改文章栏目")
	@SysLog("修改文章栏目" )
	@PostMapping("updateFxCmsColumn")
	public R updateFxCmsColumn(@RequestBody FxCmsColumn fxCmsColumn) {
		FxCmsColumn name = fxCmsColumnService.getOne(Wrappers.lambdaQuery(FxCmsColumn.class)
				.ne(FxCmsColumn::getId, fxCmsColumn.getId())
				.eq(FxCmsColumn::getName, fxCmsColumn.getName())
				.eq(FxCmsColumn::getDelFlag, DelFlagEnum.usable.getValue()));
		if (null != name) {
			return R.failed("类型名称已存在！");
		}
		fxCmsColumn.setUpdateTime(LocalDateTime.now());
		fxCmsColumn.setUpdateUser(SecurityUtils.getUserId());
		return R.ok(fxCmsColumnService.updateById(fxCmsColumn));
	}

    /**
     * 通过id删除文章栏目
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除文章栏目", notes = "通过id删除文章栏目")
    @SysLog("通过id删除文章栏目" )
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable Long id) {
        return R.ok(fxCmsColumnService.removeById(id));
    }

}
